package org.itboys.shop.intercepter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.itboys.framework.resource.ResourceHolder;
import org.itboys.framework.spring.interceptor.ServletContextInterceptor;
import org.itboys.seller.constant.SellerSessionConstant;
import org.springframework.beans.factory.annotation.Autowired;
/**
 * 登陆验证拦截器
 * @author weisky
 *
 */
public class LoginIntercepter extends ServletContextInterceptor{
	
	@Autowired
	private ResourceHolder resourceHolder;
	
	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		
		request.setAttribute("staticRoot", resourceHolder.getStringValue("staticRoot"));
		request.setAttribute("adminRoot", resourceHolder.getStringValue("adminRoot"));
		request.setAttribute("imgRoot", resourceHolder.getStringValue("imgRoot"));
		
		String url = request.getRequestURI();
		if(url.equals("/shop/yzm") || url.equals("/shop/login") || url.equals("/shop/doLogin")){
			return super.preHandle(request, response, handler);
		}else{
			try {
				Long id = (Long)request.getSession().getAttribute(SellerSessionConstant.SESSION_USER_ID);
				if (id == null) {
					response.sendRedirect("/shop/login");
					return false;
				}
			} catch (Exception e) {
				response.sendRedirect("/shop/login");
				e.printStackTrace();
				return false;
			}
		}
		
		return super.preHandle(request, response, handler);
	}



}
